package com.lovepick.util;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.NumberPicker;
import android.widget.TextView;

import com.lovepick.R;

/**
 * A dialog that prompts the nickName for the message deletion limits.
 */
public class NumberPickerDialog extends AlertDialog implements OnClickListener
{
	private static final String NUMBER = "number";

	/**
	 * 
	 * 当用户按下设置键时, 调用该回调函数以获取并设定用户所选定的值
	 */
	public interface OnNumberSetListener
	{

		/**
		 * @param number 待设置的数值
		 */
		void onNumberSet(int number);
	}

	private final NumberPicker mNumberPicker;
	private TextView mTextView;
	private final OnNumberSetListener mCallback;

	/**
	 * @param context	运行上下文
	 * @param callBack	按下确认后的回调函数
	 * @param number	默认初始设定值
	 * @param rangeMin	选择器下限
	 * @param rangeMax	选择器上限
	 * @param title		选择器标题
	 * @param unit		选择器的单位
	 */
	public NumberPickerDialog(Context context, OnNumberSetListener callBack,
			int number, int rangeMin, int rangeMax, int title, int unit)
	{
		this(context, AlertDialog.THEME_HOLO_LIGHT, callBack, number, rangeMin,
				rangeMax, title, unit);
	}

	/**
	 * @param context	运行上下文 
	 * @param theme		选择器对话框主题
	 * @param callBack	按下确认后的回调函数
	 * @param number	默认初始设定值
	 * @param rangeMin	选择器下限
	 * @param rangeMax	选择器上限
	 * @param title		选择器标题
	 * @param unit		选择器的单位
	 */
	public NumberPickerDialog(Context context, int theme,
			OnNumberSetListener callBack, int number, int rangeMin,
			int rangeMax, int title, int unit)
	{
		super(context, theme);
		mCallback = callBack;

		setTitle(title);

		setButton(DialogInterface.BUTTON_POSITIVE, context.getText(R.string.set), this);
		setButton(DialogInterface.BUTTON_NEGATIVE, context.getText(R.string.cancel), (OnClickListener) null);

		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.number_picker_dialog, null);
		setView(view);
		mNumberPicker = (NumberPicker) view.findViewById(R.id.number_picker);
		mTextView = (TextView) view.findViewById(R.id.number_unit);
		mTextView.setText(unit);
		
		// 初始化状态
		mNumberPicker.setMinValue(rangeMin);
		mNumberPicker.setMaxValue(rangeMax);
		mNumberPicker.setValue(number);
		
		// make the repeat rate three times as fast as normal since the range is so large.
		mNumberPicker.setOnLongPressUpdateInterval(100);
		// don't wrap from min->max
		mNumberPicker.setWrapSelectorWheel(false);
	}

	public void onClick(DialogInterface dialog, int which)
	{
		if (mCallback != null)
		{
			mNumberPicker.clearFocus();
			mCallback.onNumberSet(mNumberPicker.getValue());
			dialog.dismiss();
		}
	}

	@Override
	public Bundle onSaveInstanceState()
	{
		Bundle state = super.onSaveInstanceState();
		state.putInt(NUMBER, mNumberPicker.getValue());
		return state;
	}

	@Override
	public void onRestoreInstanceState(Bundle savedInstanceState)
	{
		super.onRestoreInstanceState(savedInstanceState);
		int number = savedInstanceState.getInt(NUMBER);
		mNumberPicker.setValue(number);
	}
}
